import 'package:flutter/material.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/common/store_order_btns.dart';
import 'package:xinxuan/controllers/store_order_controller/store_order_page_controller.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/utils/refresh_util.dart';
import 'package:xinxuan/vo/store_order_list_vo.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class StoreOrderGoodItem extends StatefulWidget {

  final StoreOrderListVo storeOrderListVo;
  final StoreOrderPageController storeOrderPageController;
  StoreOrderGoodItem({required this.storeOrderListVo, required this.storeOrderPageController});

  @override
  _StoreOrderGoodItemState createState() => _StoreOrderGoodItemState();
}

class _StoreOrderGoodItemState extends State<StoreOrderGoodItem> with AutomaticKeepAliveClientMixin {

  @override
  Widget build(BuildContext context) {
    super.build(context);
    if (widget.storeOrderListVo.data!.length <= 0) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(
              'assets/icons/icon_order_list_def.png',
              width: 273.w,
              height: 146.h,
            ),
            SizedBox(height: 10.h),
            Text(
              '还没订单~去下一单！',
              style: TextStyle(
                color: textSubtitleColor,
                fontSize: 14.sp
              ),
            )
          ],
        ),
      );
    }
    return RefreshUtil.easyRefresh(
      onRefresh: () async {
        widget.storeOrderPageController.getStoreTabItemOrderList(widget.storeOrderPageController.tabIndex.value);
      },
      onLoad: () async {
        widget.storeOrderPageController.onloadStoreOrderList();
      },
      slivers: [
        SliverList(
          delegate: SliverChildBuilderDelegate((context, index) {
            StoreOrderItem item = widget.storeOrderListVo.data![index];
            return InkWellUtil.shakeProofInkWell(Container(
              margin: EdgeInsets.only(bottom: 12.h),
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 8.w),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(5.w),
                  color: bgWhiteColor
                ),
                child: Column(
                  children: [
                    Container(
                      height: 44.h,
                      padding: EdgeInsets.symmetric(horizontal: 4.w),
                      decoration: BoxDecoration(
                        border: Border(
                          bottom: BorderSide(
                            width: .5,
                            color: lineColor
                          )
                        )
                      ),
                      child: Row(
                        children: [
                          Expanded(child: Text(
                            '订单号:  ${item.orderSn}',
                            overflow: TextOverflow.ellipsis,
                            maxLines: 1,
                            style: TextStyle(
                              color: textBlackColor,
                              fontSize: 14.sp,
                              fontWeight: FontWeight.bold
                            ),
                          )),
                          Text(
                            '${item.statusStr}',
                            style: TextStyle(
                              color: mainTextColor,
                              fontSize: 14.sp
                            ),
                          )
                        ],
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 12.h),
                      child: Column(
                        children: [
                          Column(
                            children: item.products!.map((productsItem) {
                              return Container(
                                margin: EdgeInsets.only(bottom: 12.h),
                                child: Row(
                                  children: [
                                    NetworkImageUtil(
                                      url: productsItem.image ?? '', 
                                      width: 76.w, 
                                      height: 76.w,
                                      borderRadius: 5.w,
                                    ),
                                    SizedBox(width: 12.w),
                                    Expanded(child: Container(
                                      constraints: BoxConstraints(
                                        minHeight: 76.w
                                      ),
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Row(
                                            children: [
                                              Expanded(child: Text(
                                                '${productsItem.title}',
                                                overflow: TextOverflow.ellipsis,
                                                maxLines: 2,
                                                style: TextStyle(
                                                  color: textBlackColor,
                                                  fontSize: 14.sp
                                                ),
                                              )),
                                              SizedBox(width: 12.w),
                                              Text(
                                                'x${productsItem.quantity}',
                                                style: TextStyle(
                                                  color: textSubtitleColor,
                                                  fontSize: 12.sp
                                                ),
                                              )
                                            ],
                                          ),
                                          SizedBox(height: 12.w),
                                          Text(
                                            '${item.createdAt}',
                                            style: TextStyle(
                                              color: textSubtitleColor,
                                              fontSize: 12.sp
                                            ),
                                          )
                                        ],
                                      ),
                                    )),
                                  ],
                                ),
                              );
                            }).toList(),
                          ),
                          Container(
                            alignment: Alignment.centerRight,
                            child: Text.rich(
                              TextSpan(
                                text: '合计支付:  ',
                                style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  fontSize: 14.sp,
                                  color: textBlackColor
                                ),
                                children: [
                                  TextSpan(
                                    text: '¥',
                                    style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 12.sp,
                                      color: textBlackColor
                                    ),
                                  ),
                                  TextSpan(
                                    text: '${item.totalAmount}',
                                    style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16.sp,
                                      color: textBlackColor
                                    ),
                                  )
                                ]
                              )
                            ),
                          )
                        ],
                      ),
                    ),
                    Container(
                      height: 60.h,
                      padding: EdgeInsets.symmetric(horizontal: 4.w),
                      decoration: BoxDecoration(
                        border: Border(
                          top: BorderSide(
                            color: lineColor,
                            width: .5
                          )
                        )
                      ),
                      child: Row(
                        children: [
                          Expanded(
                            child: item.isStar != 1 ? Container() : Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Image.asset(
                                  'assets/icons/icon_gratitude_value.png',
                                  width: 24.w,
                                  height: 24.w,
                                ),
                                Expanded(child: Text(
                                  '感恩值:${item.gratitude}',
                                  overflow: TextOverflow.ellipsis,
                                  maxLines: 1,
                                  style: TextStyle(
                                    color: textBlackColor,
                                    fontSize: 14.sp,
                                    fontWeight: FontWeight.bold
                                  ),
                                ))
                              ],
                            )
                          ),
                          item.status == 1 ? StoreOrderBtns(
                            status: item.status!, 
                            toPayFun: () async {
                              var res = await Get.toNamed(
                                '${AppRouters.COMMON_PAGE}${AppRouters.GOOD_DETAIL_PAGE}${AppRouters.STORE_CONFIRM_ORDER_PAGE}${AppRouters.STORE_ORDER_PAY_PAGE}',
                                arguments: {
                                  'orderId': item.id,
                                  'isDetail': false
                                }
                              );
                              if (res != null && res) {
                                widget.storeOrderPageController.getStoreTabItemOrderList(widget.storeOrderPageController.tabIndex.value);
                              }
                            },
                            cleanOrderFun: () {
                              widget.storeOrderPageController.storeOrderCancel(item.id!);
                            },
                            receiveOrderFun: () {
                              widget.storeOrderPageController.storeOrderReceive(item.id!);
                            }
                          ) : Container(
                            width: 90.w,
                            margin: EdgeInsets.only(left: 12.w),
                            child: ButtonUtil(
                              text: '查看订单', 
                              fontSize: 14.sp, 
                              borderRadius: 18.w, 
                              height: 36.h, 
                              onTop: () {
                                Get.toNamed(
                                  '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}${AppRouters.STORE_ORDER_DETAIL_PAGE}',
                                  arguments: {
                                    'orderId': item.id,
                                    'title': item.statusStr,
                                    'status': item.status
                                  }
                                );
                              }
                            ),
                          )
                        ],
                      )
                    )
                  ],
                ),
              ),
            ), () {
              Get.toNamed(
                '${AppRouters.COMMON_PAGE}${AppRouters.STORE_ORDER_PAGE}${AppRouters.STORE_ORDER_DETAIL_PAGE}',
                arguments: {
                  'orderId': item.id,
                  'title': item.statusStr,
                  'status': item.status
                }
              );
            });
          }, childCount: widget.storeOrderListVo.data!.length),
        )
      ]
    );
  }

  @override
  bool get wantKeepAlive => true;
}